/*
 * FileDateComparator.java
 * 
 * Copyright (C) 2010 no.good.at.coding.
 * 
 * This file is part of the NoGoodCommons Library.
 * 
 * The NoGoodCommons Library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.nogoodatcoding.commons;

import java.io.File;
import java.util.Comparator;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

/**
 * 
 * Compares files based on their timestamp. The timestamp is fetched using the FileTimes library. For Windows systems, the greater of the creation
 * time and {@link java.io.File#lastModified()} time is returned. In case of errors or in case of non-Windows systems, the {@code lastModified} time
 * is returned
 * 
 * Can sort in descending (newest first) or ascending (oldest first) order of timestamps.
 * 
 * @see com.nogoodatcoding.commons.Commons#getLatestTimestamp(java.io.File)
 * 
 * @author no.good.at.coding
 * 
 */
public class FileDateComparator implements Comparator<File>
{
    private static Logger log = Logger.getLogger(FileDateComparator.class);
    private static ResourceBundle bundle = ResourceBundle
            .getBundle("com.nogoodatcoding.commons.messages.Messages_FileDateComparator");

    private boolean isDescending = true;

    /**
     * 
     * Default constructor for convenience; assumes {@code sortDescending} is {@code true}. Will return {@code FileDateComparator} that will sort by
     * descending order of timestamps i.e. newest files first
     * 
     * @see com.nogoodatcoding.commons.FileDateComparator#FileDateComparator(boolean)
     * 
     */
    public FileDateComparator()
    {
        this.isDescending = true;
    }

    /**
     * Constructor for {@code FileDateComparator}.
     * 
     * @param sortDescending
     *            Indicates if the list is to be sorted in descending order (newest first) or not (i.e. in descending order (oldest first)).
     * 
     * @see com.nogoodatcoding.commons.FileDateComparator#FileDateComparator()
     */
    public FileDateComparator(boolean sortDescending)
    {
        this.isDescending = sortDescending;
    }

    public int compare(File file1, File file2)
    {
        if (file1 == null | file2 == null)
        {
            log.error(String.format(bundle.getString("log.error.file.null"), file1, file2));
            throw new NullPointerException();
        }

        log.trace(String.format(bundle.getString("log.trace.sort.order"), isDescending));

        long timestamp1 = Commons.getLatestTimestamp(file1);
        long timestamp2 = Commons.getLatestTimestamp(file2);
        log.trace(String.format(bundle.getString("log.trace.times"), timestamp1, timestamp2));

        if (timestamp1 < timestamp2)
        {
            if (isDescending)
            {
                log.trace(String.format("log.trace.return", 1, file2.getPath(), file1.getPath()));
                return 1;
            }
            else
            {
                log.trace(String.format("log.trace.return", -1, file1.getPath(), file2.getPath()));
                return -1;
            }

        }
        else if (timestamp1 > timestamp2)
        {
            if (isDescending)
            {
                log.trace(String.format("log.trace.return", -1, file1.getPath(), file2.getPath()));
                return -1;
            }
            else
            {
                log.trace(String.format("log.trace.return", 1, file2.getPath(), file1.getPath()));
                return 1;
            }
        }
        else
        {
            log.trace(String.format("log.trace.return.equals", 0, file1.getPath(), file2.getPath()));
            return 0;
        }
    }
}